package com.camel.chart.utils;

import org.apache.log4j.Logger;
import sun.rmi.runtime.Log;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Demo class
 *
 * @author Administrator
 * @date 2024/3/19
 */
public class FileUtil {

    private static List<FileEntity> FILE_ENTITY_LIST = new ArrayList<>();

    private static final String HISTORY_PATH = "D:/fileHistory.history";

    public static void initFileHistory(){
        deserializeFileList();
    }

    public static synchronized void addFile(FileEntity fileEntity){
        FILE_ENTITY_LIST.add(fileEntity);
    }

    public static void clearFileList(){
        FILE_ENTITY_LIST.clear();
    }

    public static List<FileEntity> getFileEntityList(){
        return FILE_ENTITY_LIST;
    }

    public static void serializeFileList() {
        try (FileOutputStream fileOutputStream = new FileOutputStream(HISTORY_PATH);
             ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream)){
            objectOutputStream.writeObject(FILE_ENTITY_LIST);
            System.out.println("保存文件历史成功,路径: " + HISTORY_PATH);
            Thread.sleep(200);
        }catch (IOException | InterruptedException e){
            e.printStackTrace();
        }
    }

    public static void deserializeFileList(){
        try(FileInputStream fileInputStream = new FileInputStream(HISTORY_PATH);
            ObjectInputStream os = new ObjectInputStream(fileInputStream)){
            clearFileList();
            FILE_ENTITY_LIST = (List<FileEntity>)os.readObject();
        }catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }

    }

    public static class FileEntity implements Serializable {

        private String fileId;
        private String userId;
        private transient File file;
        private String filePath;
        private String fileName;
        private long fileSize;
        private static final long serialVersionUID = 5098379834429301459L;

        public String getFileId() {
            return fileId;
        }

        public void setFileId(String fileId) {
            this.fileId = fileId;
        }

        public String getUserId() {
            return userId;
        }

        public void setUserId(String userId) {
            this.userId = userId;
        }

        public File getFile() {
            return file;
        }

        public void setFile(File file) {
            this.file = file;
        }

        public String getFilePath() {
            return filePath;
        }

        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }

        public String getFileName() {
            return fileName;
        }

        public void setFileName(String fileName) {
            this.fileName = fileName;
        }

        public long getFileSize() {
            return fileSize;
        }

        public void setFileSize(long fileSize) {
            this.fileSize = fileSize;
        }

        @Override
        public String toString() {
            return "FileEntity{" +
                    "fileId='" + fileId + '\'' +
                    ", userId='" + userId + '\'' +
                    ", file=" + file +
                    ", filePath='" + filePath + '\'' +
                    ", fileName='" + fileName + '\'' +
                    ", fileSize=" + fileSize +
                    '}';
        }
    }

}
